﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using SaaS.Core.Authorization;
using XCore.Authorization;
using XCore.Logger;
using XCore.Extensions;
using XCore.Helper;

namespace Web.System.Controllers.Logs
{
    /// <summary>
    /// 审计日志
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class LogController : SystemControllerBase
    {
        #region 工具Api日志
        [HttpPost("utils/list"), DisableAuditing, Permission(PermissionNames.Log)]
        public ActionResult UtilsList()
        {
            var query = LDC.UtilLogs.AsQueryable();
            return PageResult(query.OrderByDescending(c => c.CreationTime), c => new
            {
                c.Id,
                c.Duration,
                c.Message,
                c.SecretKey,
                c.Level
            });
        }
        [HttpGet("utils/{id}"), DisableAuditing, Permission(PermissionNames.Log)]
        public ActionResult UtilsInfo(int id)
        {
            return Ok(LDC.UtilLogs.FirstOrDefault(c => c.Id == id));
        }
        #endregion

        #region 文件日志
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("file/list"), DisableAuditing, Permission(PermissionNames.Log_File)]
        public ActionResult FileList()
        {
            var query = LDC.FileLogs.AsQueryable();
            long length = query.Sum(c => c.Length);
            return PageResult(query.OrderByDescending(c => c.CreationTime), c => new
            {
                c.CreationTime,
                c.Folder,
                c.Id,
                c.Length,
                c.Name,
                c.Path,
                c.Type
            }, length);
        }
        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("file/{id}"), DisableAuditing, Permission(PermissionNames.Log_File)]
        public ActionResult FileInfo(long id)
        {
            return Ok(LDC.FileLogs.FirstOrDefault(c => c.Id == id));
        }
        #endregion

        #region 错误日志


        [HttpPost("error/list"), DisableAuditing, Permission(PermissionNames.Log_Error)]
        public ActionResult ErrorList([FromForm]DateTime? startTime, [FromForm]DateTime? endTime)
        {
            var query = LDC.ErrorLogs.Where(startTime, c => c.CreationTime >= startTime)
                                     .Where(endTime, c => c.CreationTime <= endTime);
            return PageResult(query.OrderByDescending(c => c.CreationTime), c => new
            {
                c.Appdomin,
                c.CreationTime,
                c.Id,
                c.Message,
            });
        }

        [HttpGet("error/{id}"), DisableAuditing, Permission(PermissionNames.Log_Error)]
        public ActionResult ErrorInfo(long id)
        {
            return Ok(LDC.ErrorLogs.FirstOrDefault(c => c.Id == id));
        }

        #endregion

        #region 跟踪日志

        [HttpPost("trace/list"), DisableAuditing, Permission(PermissionNames.Log_Trace)]
        public ActionResult TraceListt([FromForm]DateTime? startTime, [FromForm]DateTime? endTime)
        {
            var query = LDC.TraceLogs.Where(startTime, c => c.CreationTime >= startTime)
                                     .Where(endTime, c => c.CreationTime <= endTime);
            return PageResult(query.OrderByDescending(c => c.CreationTime), c => new
            {
                c.CreationTime,
                c.IP,
                ClientAddress = IPHelper.GetAddress(c.IP),
                c.Duration,
                c.Id,
                c.Path,
                c.Type
            });
        }
        [HttpGet("trace/{id}"), DisableAuditing, Permission(PermissionNames.Log_Trace)]
        public ActionResult TraceInfo(long id)
        {
            return Ok(LDC.TraceLogs.FirstOrDefault(c => c.Id == id));
        }

        #endregion

        #region 调度器日志

        [HttpPost("scheduler/list"), DisableAuditing, Permission(PermissionNames.Log_Scheduler)]
        public ActionResult SchedulerListt([FromForm]DateTime? startTime, [FromForm]DateTime? endTime)
        {
            var query = LDC.SchedulerLogs.Where(startTime, c => c.CreationTime >= startTime)
                                       .Where(endTime, c => c.CreationTime <= endTime);
            return PageResult(query.OrderByDescending(c => c.CreationTime), c => new
            {
                c.Count,
                c.CreationTime,
                c.Duration,
                c.Message,
                c.Name,
                c.Id
            });
        }
        #endregion

    }
}